package com.mj.mafeed.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.mj.dollars.AppConnect;
import com.mj.mafeed.R;
import com.mj.mafeed.adapter.CartItemAdapter;
import com.mj.mafeed.bean.CartItemJSON;
import com.mj.mafeed.widget.LoadingDialog;
import com.mj.mafeed.util.SPUtils;
import com.mj.mafeed.bean.CartItemListJSON;
import com.mj.mafeed.service.CartItemService;
import com.mj.mafeed.util.NetUtils;
import com.mj.mafeed.activity.CheckoutActivity;
import com.mj.mafeed.activity.LoginActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 购物车
 * @author zhaominglei
 * @date 2016-1-10
 * 
 */
public class CartActivity extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = CartActivity.class.getSimpleName();
	private CartItemService cartItemService = new CartItemService();
	private boolean isExit = false;
	private TimerTask timerTask;
	private RelativeLayout nologinLayout;
	private RelativeLayout nologinMessageLayout;
	private RelativeLayout loginLayout;
	private Button loginBtn; //登录
	private ImageView shoppingBtn; //去逛逛
	private LoadingDialog loadingDialog; //加载框
	private ListView cartItemListView;
	private List<CartItemJSON> cartItems = new ArrayList<CartItemJSON>();
	private CartItemAdapter cartItemAdapter;
	@SuppressWarnings("unused")
	private String referer; //来自那个链接
	private String username; //用户名
	private String sign; //签名
	private RelativeLayout totalCheckout;
	private TextView totalText; //总计
	private double total = 0.0d; //总计金额
	private Button checkoutBtn; //去结算
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_cart);
		init();
	}
	
	private void init() {
		nologinLayout = (RelativeLayout)findViewById(R.id.cart_nologin);
		nologinMessageLayout = (RelativeLayout)findViewById(R.id.cart_nologin_message);
		loginBtn = (Button)findViewById(R.id.cart_login);
		shoppingBtn = (ImageView)findViewById(R.id.cart_goshopping);
		loginLayout = (RelativeLayout)findViewById(R.id.cart_login_layout);
		cartItemListView = (ListView)findViewById(R.id.cart_cart_item_listview);
		totalCheckout = (RelativeLayout)findViewById(R.id.cart_total_checkout);
		totalText = (TextView)findViewById(R.id.cart_total);
		checkoutBtn = (Button)findViewById(R.id.cart_checkout);
		
		loginBtn.setOnClickListener(this);
		shoppingBtn.setOnClickListener(this);
		checkoutBtn.setOnClickListener(this);
		loadingDialog = new LoadingDialog(this);
		
		nologinLayout.setVisibility(View.VISIBLE);
		nologinMessageLayout.setVisibility(View.VISIBLE);
		loginLayout.setVisibility(View.GONE);
		referer = getIntent().getStringExtra("referer");
		username = (String)SPUtils.get(getApplicationContext(), "username", "");
		sign = (String)SPUtils.get(getApplicationContext(), "sign", "");
		if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
			nologinLayout.setVisibility(View.GONE);
			nologinMessageLayout.setVisibility(View.GONE);
			loginLayout.setVisibility(View.VISIBLE);
		} 
		cartItemAdapter = new CartItemAdapter(this, cartItems);
		cartItemListView.setAdapter(cartItemAdapter);
		getCartItems();
	}

	private void getCartItems() {
		if (NetUtils.isConnected(getApplicationContext())) {
			loadingDialog.show();
			new CartItemAsyncTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cart_login:
			intent = new Intent(CartActivity.this, LoginActivity.class);
			intent.putExtra("referer", "CartActivity");
			startActivity(intent);
			CartActivity.this.finish();
			break;
		case R.id.cart_goshopping:
			intent = new Intent(CartActivity.this, MainActivity.class);
			intent.putExtra("referer", "CartActivity");
			intent.putExtra("gowhere", "goshopping");
			startActivity(intent);
			CartActivity.this.finish();
			break;
		case R.id.cart_checkout:
			if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
				intent = new Intent(CartActivity.this, CheckoutActivity.class);
				intent.putExtra("referer", "CartActivity");
				intent.putExtra("total", total);
				startActivity(intent);
				CartActivity.this.finish();
			} else {
				intent = new Intent(CartActivity.this, LoginActivity.class);
				intent.putExtra("referer", "CartActivity");
				startActivity(intent);
			}
			break;			

		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		if (isExit) {
			AppConnect.getInstance(this).close();
			CartActivity.this.finish();
		} else {
			isExit = true;
			Toast.makeText(CartActivity.this, R.string.exit_msg, Toast.LENGTH_SHORT).show();
			timerTask = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			new Timer().schedule(timerTask, 2*1000);
		}
	}
	
	public class CartItemAsyncTask extends AsyncTask<String, String, CartItemListJSON> {
		@Override
		protected CartItemListJSON doInBackground(String... params) {
			return cartItemService.syncAndGetAll(getApplicationContext());
		}
		@Override
		protected void onPostExecute(CartItemListJSON result) {
			loadingDialog.dismiss();
			if (result != null && result.getRecordList() != null && result.getRecordList().size() > 0) {
				nologinLayout.setVisibility(View.GONE);
				nologinMessageLayout.setVisibility(View.GONE);
				totalCheckout.setVisibility(View.VISIBLE);
				loginLayout.setVisibility(View.VISIBLE);
				
				cartItemAdapter.setCartItems(result.getRecordList());
				cartItemAdapter.notifyDataSetChanged();
				List<CartItemJSON> recordList = result.getRecordList();
				for (CartItemJSON cartItemJSON : recordList) {
					total += cartItemJSON.getPrice().doubleValue()*cartItemJSON.getQuantity();
				}
				totalText.setText(String.valueOf(total));
			} else {
				nologinMessageLayout.setVisibility(View.VISIBLE);
				if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
					nologinLayout.setVisibility(View.GONE);
				} else {
					nologinLayout.setVisibility(View.VISIBLE);
				}
				totalCheckout.setVisibility(View.GONE);
			}
		}
	}	
}
